<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <!-- 表格区 -->
      <el-table :data="cateList" border stripe>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="primary" size="mini" @click="showUpdate(row)">修改</el-button>
            <el-button type="danger" size="mini" @click="remove(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 对话框 -->
    <el-dialog
      @closed="onAddClosed"
      title="添加分类"
      :visible.sync="addVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <!-- 添加分类的FORM表单 -->
      <el-form :model="addForm" label-width="80px" :rules="formRules" ref="addFormRef">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addNewCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改的对话框 -->
    <el-dialog title="修改分类" :visible.sync="editVisibel" width="35%">
      <!-- 修改分类表单 -->
      <el-form :model="editForm" :rules="formRules" label-width="80px" ref="editFormRef">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisibel = false">取 消</el-button>
        <el-button type="primary" @click="updateCate">修 改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCateListAPI, addCateAPI, updateCateAPI, deleteAPI } from '@/api/article.js'

export default {
  name: 'ArtCate',
  data() {
    return {
      cateList: [],
      addVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      formRules: {
        cate_name: [
          { required: true, message: '分类名称是必填项', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称必须是1-10位非空字符串', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类名称是必填项', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类名称必须是1-15位非空字符串', trigger: 'blur' }
        ]
      },
      editVisibel: false,
      editForm: {
        id: '',
        cate_name: '',
        cate_alias: ''
      }
    }
  },
  created() {
    this.initCateList()
  },
  methods: {
    async initCateList() {
      const { data: res } = await getCateListAPI()
      if (res.code !== 0) return this.$message.error('文章获取失败')
      this.cateList = res.data
    },
    onAddClosed() {
      this.$refs.addFormRef.resetFields()
    },
    addNewCate() {
      this.$refs.addFormRef.validate(async valid => {
        const { data: res } = await addCateAPI(this.addForm)
        if (res.code !== 0) return this.$message.error('新增文章失败')
        this.$message.success('添加文章成功')
        this.addVisible = false
        this.initCateList()
      })
    },
    showUpdate(row) {
      // 判断ID是1或2时，不允许修改
      if (row.id === 1 || row.id === 2) return this.$message.error('管理员不允许修改这个分类！')
      this.editForm = Object.assign({}, row)
      this.editVisibel = true
    },
    // 修改文章分类
    updateCate() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await updateCateAPI(this.editForm)
        if (res.code !== 0) return this.$message.error('更新分类失败')
        this.$message.success('更新分类成功')
        this.editVisibel = false
        this.initCateList()
      })
    },
    async remove(id) {
      if (id === 1 || id === 2) return this.$message.error('管理员不允许删除')
      const result = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result === 'cancel') return
      const { data: res } = await deleteAPI(id)
      if (res.code !== 0) return this.$message.error('删除分类失败')
      this.$message.success('删除分类成功')
      this.initCateList()
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
